package com.nnxy.worktostudy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnxy.worktostudy.system.dto.response.ApplyListItem;
import com.nnxy.worktostudy.system.entity.Apply;
import com.nnxy.worktostudy.system.entity.Job;
import com.nnxy.worktostudy.system.entity.Project;
import com.nnxy.worktostudy.system.entity.Student;
import com.nnxy.worktostudy.system.mapper.ApplyMapper;
import com.nnxy.worktostudy.system.service.ApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nnxy.worktostudy.system.service.JobService;
import com.nnxy.worktostudy.system.service.ProjectService;
import com.nnxy.worktostudy.system.service.StudentService;
import com.nnxy.worktostudy.utils.ResponseFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luye
 * @since 2021-10-14
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {

    @Resource
    private StudentService studentService;
    @Resource
    private JobService jobService;
    @Resource
    private ProjectService projectService;

    @Override
    public Map<String,Object> page(Wrapper<Apply> queryWrapper, Integer pageSize, Integer currentPage){
        IPage<Apply> page = new Page<>();
        Map<String,Object> result = new HashMap<>(3);
        List<ApplyListItem> applyListItems = new ArrayList<>();

        page.setSize(pageSize);
        page.setCurrent(currentPage);
        page = this.page(page,queryWrapper);

        result.put("currentPage", page.getCurrent());
        result.put("total", page.getTotal());

        for (Apply apply: page.getRecords()) {
            ApplyListItem applyListItem = new ApplyListItem();
            Student student = studentService.getById(apply.getStudentId());
            Job job = jobService.getById(apply.getJobId());
            Project project = projectService.getById(job.getProjectId());

            applyListItem.setApplyTime(apply.getCreateTime());
            applyListItem.setProjectName(project.getName());
            applyListItem.setJob(job);
            applyListItem.setStudent(student);
            applyListItems.add(applyListItem);
        }

        result.put("data", applyListItems);

        return result;
    }
}
